{__NOLAYOUT__}
<layout name="no_nav" />

<!-- TOPBAR -->
<header class="top-bar flex-row">
  <a class="row-item row-item-auto top-bar-back" href="javascript:window.history.back()">返回</a>
  <h3 class="row-item top-bar-name">收货地址</h3>
  <span class="row-item row-item-auto top-bar-menu"></span>
</header>

<empty name="Think.get.buyUrl">

<!-- 收货地址列表 -->
<div id="deliver-address" class="lists">
  <volist name="deliverAddressLists" id="vo">
  <div class="lists-item">
    <a class="lists-item-link" href="__CONTROLLER__/modifyDeliverAddress/sid/{$sid}/id/{$vo.id}">
      <p><strong>{$vo.name}</strong>&nbsp;&nbsp;{$vo.tel}</p>
      <p>{$vo.address}</p>
      <a class="address-del-btn btn btn-red transition" data-id="{$vo.id}">删除</a>
    </a>
  </div>
  </volist>
</div>

<section class="lists">
  <div class="lists-item">
    <a class="lists-item-link" href="__CONTROLLER__/addDeliverAddress/sid/{$sid}">添加新地址</a>
  </div>
</section>

<script type="text/javascript">
  //删除地址功能能，左滑出现删除按钮，右滑消失，点击跳转。
  (function(){

    $("#deliver-address").on("tap swipeLeft swipeRight", ".lists-item", bindTouch);
    $("#deliver-address").on("tap", ".address-del-btn", delAddress);

    function bindTouch( e ) {
      var _this = this
      switch ( e.type ) {
        case "swipeLeft" :
          transitionDel(e, _this, true);
          break;
        case "swipeRight" :
          transitionDel(e, _this, false);
          break;
      };
    };

    function transitionDel ( e, self, show ) {
      e.preventDefault();
      show ? $(self).find(".address-del-btn").addClass("active")
           : $(self).find(".address-del-btn").removeClass("active");
    };

    function delAddress ( e ) {

      e.stopPropagation();

      var _this = this;
      require(["modal"], function(M) {
        M.confirm({
          title: "删除地址提示",
          text: "删除后无法恢复，确定要删除吗？",
          buttons: [{
            text: "取消",
            close: true
          }, {
            text: "删除",
            close: true,
            click: function() {
              del.call(_this);
            }
          }]
        })
      });
      
    };

    function del() {
      var id = $(this).attr("data-id"),
          sid = {$sid},
          _this = this;
      $.post("__CONTROLLER__/deleteDeliverAddress", {id: id, sid: sid}, function(response) {
        APP.alert(response.msg);
        if ( response.error == 0 ) {
          $(_this).parent().remove();
        }
      });
    }

  })();
</script>

<else/>

<!-- 收货地址的选择 -->
<div id="deliver-address" class="deliver-address">
  
  <volist name="deliverAddressLists" id="vo">
  <section class="deliver-address-item {$vo['status']?' active':''}" data-address-id="{$vo.id}">
    <p>{$vo.address}</p>
    <p>{$vo.name}&nbsp;&nbsp;{$vo.tel}</p>
  </section>
  </volist>

  <a class="btn btn-orange w100" href="__CONTROLLER__/addDeliverAddress/sid/{$sid}?buyUrl={$buyUrl}">新增收货地址</a>

</div>
<script type="text/javascript">
  $(function(){
    var buyURL = "{$_GET['buyUrl']}";

    $("#deliver-address").on("tap", ".deliver-address-item", function(){

      $(this).addClass("active").siblings().removeClass("active");

      var deliverId = $(this).attr("data-address-id"),
          sid = {$sid};
      $.post("__CONTROLLER__/updateAddressStatus", { deliverId: deliverId, sid: sid }, function(response){
        response.error === 0 && ( window.location = buyURL );
      });

    });
  });
</script>

</empty>


